package com.bdi.mqtt_broker.service.impl;

import com.bdi.mqtt_broker.service.RedisService;
import com.bdi.mqtt_broker.utils.JedisConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Map;
import java.util.Set;

/**
 * @author WangNing yogehaoren@gmial.com <br>
 * @since 1.0
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    JedisConnection jedisConnection;

    @Override
    public Long hset(String key, String field, String value) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.hset(key, field, value);
        }
    }


    @Override
    public String hget(String key, String field) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.hget(key, field);
        }
    }

    @Override
    public Map<String, String> hgetAll(String key) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.hgetAll(key);
        }
    }

    @Override
    public Boolean hexists(String key, String field) {

        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.hexists(key, field);
        }

    }

    @Override
    public Long hdel(String key, String... fields) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.hdel(key, fields);
        }
    }

    @Override
    public Long sadd(String key, String... members) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.sadd(key, members);
        }
    }

    @Override
    public Set<String> smembers(String key) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.smembers(key);
        }
    }

    @Override
    public Long srem(String key, String... members) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.srem(key, members);
        }
    }

    @Override
    public Long del(String key) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.del(key);
        }
    }

    @Override
    public String set(String key, String value) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.set(key, value);
        }
    }

    @Override
    public String get(String key) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.get(key);
        }
    }

    @Override
    public Boolean exists(String key) {
        try(Jedis jedis = jedisConnection.getJedis()){
            return jedis.exists(key);
        }
    }
}
